
var testVersionCheck=41;

declare function hideADS();
declare function getBrowserVersion();


class LoadingUI extends egret.Sprite {

    private labProgress:egret.TextField;
    private labSound:egret.TextField;
    private labMusic:egret.TextField;
    private loadFinished:boolean = false;
    private mainDoc:Main;

    public constructor(md:Main) {
        super();
        this.mainDoc = md;
        this.createView();
    }

    public static isSoundOn(){
        var cso = readCookie("soundOn");
        return (cso==null)||(cso=="1");
    }

    public static isMusicOn(){
        var mso = readCookie("musicOn");
        return (mso==null)||(mso=="1");
    }

    private enableSound(b:boolean){
        this.labSound.text = (!b)?"音效：关":"音效：开";
        this.labSound.backgroundColor = (!b)?0xee3300:0x00ee33;
        createCookie("soundOn",(!b)?"0":"1",10);
    }

    private enableMusic(b:boolean){
        this.labMusic.text = (!b)?"音乐：关":"音乐：开";
        this.labMusic.backgroundColor = (!b)?0xee3300:0x00ee33;
        createCookie("musicOn",(!b)?"0":"1",10);
    }

    private createView():void {
        var stage = egret.MainContext.instance.stage
        var stageW = stage.stageWidth
        var stageH = stage.stageHeight

        this.graphics.beginFill(0x333333, 0.9);
        this.graphics.drawRect(0, 0, stageW, stageH);
        this.graphics.endFill();

        this.labProgress = new egret.TextField();
        this.labProgress.y = 50;
        this.labProgress.width = stageW;
        this.labProgress.size = 50;
        this.labProgress.text = "加载中，请稍等...";
        this.labProgress.textAlign = "center";
        this.addChild(this.labProgress);

        var soundOn = LoadingUI.isSoundOn();
        var labSound = new egret.TextField();
        labSound.x = 80; labSound.y = 130; labSound.width = 200; labSound.height = 60;
        labSound.size = 40; labSound.textAlign = "center"; labSound.verticalAlign = "middle";
        labSound.background = true; labSound.stroke = 2; labSound.strokeColor = 0;
        labSound.touchEnabled = true;
        labSound.addEventListener(egret.TouchEvent.TOUCH_BEGIN , function(e:egret.Event){
            e.stopPropagation();
            soundOn = !soundOn;
            console.log(soundOn);
            this.enableSound(soundOn);
        }, this);
        this.labSound = labSound;
        this.addChild(labSound);
        this.enableSound(soundOn);

        var musicOn = LoadingUI.isMusicOn();
        var labMusic = new egret.TextField();
        labMusic.x = 360; labMusic.y = 130; labMusic.width = 200; labMusic.height = 60;
        labMusic.size = 40; labMusic.textAlign = "center"; labMusic.verticalAlign = "middle";
        labMusic.background = true; labMusic.stroke = 2; labMusic.strokeColor = 0;
        labMusic.touchEnabled = true;
        labMusic.addEventListener(egret.TouchEvent.TOUCH_BEGIN , function(e:egret.Event){
            e.stopPropagation();
            musicOn = !musicOn;
            console.log(musicOn);
            this.enableMusic(musicOn);
        }, this);
        this.labMusic = labMusic;
        this.addChild(labMusic);
        this.enableMusic(musicOn);

        var labGuide = new egret.TextField();
        labGuide.y = 180;
        labGuide.width = stageW;
        labGuide.size = 35;
        labGuide.textAlign = "center";
        this.addChild(labGuide);

        var parser:egret.HtmlTextParser = new egret.HtmlTextParser();
        var text = "\n";
        text += "<font color=#ff0000 size=70 stroke=2><b>玩法说明</b></font>"
        text += "\n\n"
        text += "1、上下左右滑动，类似于2048";
        text += "\n\n"
        text += "2、<font color=#0000ff stroke=2 strokeColor=0xff0000>蓝色</font>是我军，<font color=#ffff00 stroke=2>黄色</font>是日军！";
        text += "\n\n";
        text += "3、相同数字碰撞即合并叠加，\n颜色变为<font color=0xff0000>主动撞击者</font>颜色";
        text += "\n\n";
        text += "4、只有我军消灭日军才计分！";
        labGuide.textFlow = parser.parser(text);

        var y = 750;
        var text:string = readCookie("lastGame");
        if( text!=null )
        {
            var labLoad = new egret.TextField();
            y = 650;
            labLoad.y = y;
            labLoad.width = stageW;
            labLoad.height = 100;
            labLoad.size = 40;
            labLoad.textAlign = "center";
            labLoad.verticalAlign = "middle";
            labLoad.text = "发现你上局没有玩完，\n点此继续游戏";
            labLoad.backgroundColor = 0xee3300;
            labLoad.background = true;
            labLoad.touchEnabled = true;
            labLoad.addEventListener(egret.TouchEvent.TOUCH_BEGIN , function(e:egret.Event){
                if(!this.loadFinished) return;
                this.close();
                e.stopPropagation();
            }, this);

            this.addChild(labLoad);
            y += 150;
        }

        var bv = getBrowserVersion();
        var labBegin = new egret.TextField();
        labBegin.y = y;
        labBegin.width = stageW;
        labBegin.size = 40;
        labBegin.textAlign = "center";
        labBegin.text = "点击开始新游戏";
        //labBegin.text += bv["android"];
        this.addChild(labBegin);

        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_BEGIN , function(e:egret.Event){
            if(!this.loadFinished) return;
            createCookie("lastGame", "", -1);
            this.mainDoc.resetGame();
            this.close();
        }, this);
    }

    public setProgress(current, total):void {
        this.labProgress.text = "加载中："+ current + "/" + total;
        if(current>=total)
        {
            this.labProgress.text = "加载已完成！";
            this.labProgress.text += "(版本"+testVersionCheck+")";
            this.loadFinished = true;
        }
    }

    private close(){
        hideADS();
        this.parent.removeChild(this);
        if( LoadingUI.isMusicOn() )
            this.mainDoc.playMusic();
    }
}
